package com.itheima.Service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.CheckgroupService;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CheckgroupServiceImpl implements CheckgroupService {

    @Autowired
    private CheckgroupMapper checkgroupMapper;

    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 新增检查组
     * @param checkGroup
     * @return
     */
    public void add(CheckGroup checkGroup,String checkitemIds) {
        List<CheckItem> lists = new ArrayList<>();
        String[] split = checkitemIds.split(",");
        for (String s : split) {
            CheckItem checkItem = checkItemMapper.findById(Integer.valueOf(s));
            lists.add(checkItem);
        }
        checkGroup.setCheckItems(lists);
        checkgroupMapper.add(checkGroup);
    }

    /**
     * 根据id删除检查组
     * @param id
     */
    public void deleteById(String id) {
        checkgroupMapper.deleteById(id);
    }

    /**
     * 根据套餐id查询关联的检查组id
     * @param setmealId
     * @return
     */
    public List<Integer> findCheckGroupIdsBySetmealId(String setmealId) {
       List<Integer> lists =  checkgroupMapper.findCheckGroupIdsBySetmealId(setmealId);
       return lists;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        int pageNum = queryPageBean.getCurrentPage();
        int pageSize = queryPageBean.getPageSize();
        // 开启pageHelper
        PageHelper.startPage(pageNum,pageSize);
        Page<CheckGroup> page = checkgroupMapper.findPage(queryPageBean);
        return new PageResult((page.getTotal()),page.getResult());
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param checkitemIds
     */
    public void edit(CheckGroup checkGroup, String checkitemIds) {
        List<CheckItem> lists = new ArrayList<>();
        String[] split = checkitemIds.split(",");
        for (String s : split) {
            CheckItem checkItem = checkItemMapper.findById(Integer.valueOf(s));
            lists.add(checkItem);
        }
        checkGroup.setCheckItems(lists);
        checkgroupMapper.edit(checkGroup);
    }

    /**
     * 查询所有检查组
     * @return
     */
    public List<CheckGroup> findAll() {
        List<CheckGroup> lists = checkgroupMapper.findAll();
        return lists;
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    public CheckGroup findById(String id) {
        CheckGroup checkGroup = checkgroupMapper.findById(id);
        return checkGroup;
    }
}
